import Cookie from 'js-cookie'
export default {
    state: {
        isCollapse: false,/* 用于控制菜单的展开或者收起 */
        tabList: [
            {
                path: '/',
                name: 'home',
                label: '首页',
                icon: 's-home',
                url: 'Home/Home'
            },
        ],/* 面包屑 */
        menu: [],
    },
    mutations: {
        /* 修改菜单展开or折起的方法 */
        collapseMenu(state) {
            state.isCollapse = !state.isCollapse
        },
        /* 更新面包屑数据 */
        selectMenu(state, val) {
            if (val.name != 'home') {
                const index = state.tabList.findIndex(item => item.name === val.name)
                if (index === -1) {
                    state.tabList.push(val)
                }
            }
        },

        /* 移出某一个 */
        deletMenu(state, val) {
            state.tabList.splice(state.tabList.indexOf(val), 1);
        },
        /* 设置menu的数据 */
        setMenu(state, val) {
            state.menu = val
            Cookie.set('menu', JSON.stringify(val))
        },
        /* 清除面包屑 */
        handtableList(state) {
            state.tabList = [{ path: '/', name: 'home', label: '首页', icon: 's-home', url: 'Home/Home' },]
        },
        addMenu(state, router) {
            /* 判断当前缓存中是否有数据 */
            if (!Cookie.get('menu')) return;
            const menu = JSON.parse(Cookie.get('menu'))
            state.menu = menu
            // /* 组装动态路由的数据 */
            const menuArray = []
            menu.forEach(item => {
                if (item.children) {
                    item.children = item.children.map(item => {
                        item.component = () => import(`../view/${item.url}`)
                        return item
                    })
                    menuArray.push(...item.children)
                } else {
                    item.component = () => import(`../view/${item.url}`)
                    menuArray.push(item)
                }
            })
            menuArray.forEach(item => {
                router.addRoute('Main', item)
            })
        }
    }
}